package ui.widget;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;

import ui.util.FontUtil;

public class YPUIBtn extends CommonStyleBtn {

	private Color defaultColor;
	private Color mouseInColor;
	private Color fontColor;
	public static final Color DFCOLOR = new Color(72, 160, 114);
	public static final Color MICOLOR = new Color(72, 180, 114);
	private Font font;

	public YPUIBtn() {
		defaultColor = new Color(72, 160, 114);
		mouseInColor = new Color(72, 180, 114);
		fontColor = Color.white;
		font = new Font(null, Font.BOLD, 15);
		super.setFont(font);
	}

	public YPUIBtn(String txt) {
		this();
		setText(txt);
	}

	public Color getFontColor() {
		return fontColor;
	}

	public void setFontColor(Color fontColor) {
		this.fontColor = fontColor;
		repaint();
	}

	public Font getFont() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
		super.setFont(font);
		repaint();
	}

	public Color getDefaultColor() {
		return defaultColor;
	}

	public void setDefaultColor(Color defaultColor) {
		this.defaultColor = defaultColor;
		repaint();
	}

	public Color getMouseInColor() {
		return mouseInColor;
	}

	public void setMouseInColor(Color mouseInColor) {
		this.mouseInColor = mouseInColor;
		repaint();
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		int h = getHeight();
		int w = getWidth();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, w - 1, h - 1, 5, 5);
		Shape clip = g2d.getClip();
		g2d.clip(r2d);
		g2d.setColor(defaultColor);
		if (isEnter) {
			g2d.setColor(mouseInColor);
		}
		g2d.fillRect(0, 0, w, h);
		g2d.setClip(clip);
		g2d.drawRoundRect(0, 0, w - 1, h - 1, 5, 5);
		g2d.drawRoundRect(1, 1, w - 3, h - 3, 4, 4);
		g2d.setColor(fontColor);
		int fw = (int) FontUtil.getFontWidth(g2d, font, getText());
		int fh = (int) FontUtil.getFontHeight(g2d, font);
		int x = (w - fw) / 2;
		int y = (h - fh) / 2 + fh-4;
		g2d.drawString(getText(), x, y);
		g2d.dispose();
	}
}
